<template>
  <div>
    <el-card>
      <Category @getCategoryId="getCategoryId"></Category>
    </el-card>
    <el-card style="margin: 10px 0">
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!category3Id"
          @click="addAttr"
          >添加属性</el-button
        >
        <el-table :data="attrList" style="width: 100%; margin: 10px 0" border>
          <el-table-column type="index" label="序号" width="90" align="center">
          </el-table-column>
          <el-table-column prop="attrName" label="属性名称" width="width">
          </el-table-column>
          <el-table-column label="属性值列表" width="800">
            <template slot-scope="{ row, $index }">
              <!-- row平台属性:展示平台属性值 -->
              <el-tag
                type="success"
                v-for="attr in row.attrValueList"
                :key="attr.id"
                style="margin: 0px 5px"
                hit
                >{{ attr.valueName }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="{ row, $index }">
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                @click="updateAttr(row)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 添加属性的场景 -->
      <div v-show="scene == 1">
        <!-- 行内form -->
        <el-form :inline="true" ref="form" :model="attrForm">
          <el-form-item label="属性名">
            <el-input v-model="attrForm.attrName"></el-input>
          </el-form-item>
        </el-form>
        <!-- 两个button按钮 -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!(attrForm.attrName.length > 0)"
          @click="addAttrValue"
          >添加属性值</el-button
        >
        <el-button @click="scene = 0">取消</el-button>
        <!-- table -->
        <el-table
          style="width: 100%; margin: 20px 0"
          border
          :data="attrForm.attrValueList"
        >
          <el-table-column
            type="index"
            label="序号"
            width="80px"
            align="center"
          >
          </el-table-column>

          <el-table-column label="属性值名称" width="width">
            <!-- 编辑模式与查看模式 -->
            <template slot-scope="{ row, $index }">
              <el-input
                v-model="row.valueName"
                :ref="$index"
                size="mini"
                v-if="row.show"
                @blur="toLook(row)"
                @keyup.enter.native="toLook(row)"
              ></el-input>
              <span v-else @click="toEidt(row, $index)">{{
                row.valueName
              }}</span>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="attrForm.attrValueList.splice($index, 1)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button
          type="primary"
          :disabled="this.attrForm.attrValueList.length < 1"
          @click="addOrUpdateSave"
          >保存</el-button
        >
        <el-button @click="scene = 0">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import cloneDeep from "lodash/cloneDeep";
export default {
  name: "Attr",
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      // 存储品牌数据
      attrList: [],
      // 切换场景
      scene: 0,
      // 收集数据
      attrForm: {
        // 属性的名字
        attrName: "",
        // 属性值的地方
        attrValueList: [
          // {
          //   attrId: 0,//属性的id
          //   valueName: "", //属性值的名称
          // },
        ],
        categoryId: "", //三级属性的id
        categoryLevel: 3,
      },
    };
  },
  methods: {
    // 平台属性父组件的方法(子组件传入的数据)
    getCategoryId({ id, flag }) {
      switch (flag) {
        case 1:
          this.category1Id = id;
          break;
        case 2:
          this.category2Id = id;
          break;
        case 3:
          this.category3Id = id;
          this.getAttrList();
          break;
      }
    },
    // 获取品牌数据的函数
    async getAttrList() {
      // 整理参数
      let { category1Id, category2Id, category3Id } = this;
      let result = await this.$ajax.attr.reqAttrList(
        category1Id,
        category2Id,
        category3Id
      );
      // 获取参数
      if (result.code == 200) {
        this.attrList = result.data;
      }
    },
    // 添加属性|切换场景
    addAttr() {
      this.scene = 1;
      this.attrForm = {
        attrName: "",
        // 属性值的地方
        attrValueList: [
          // {
          //   attrId: 0,//属性的id
          //   valueName: "", //属性值的名称
          // },
        ],
        categoryId: this.category3Id,
        categoryLevel: 3,
      };
    },
    // 修改按钮的回调
    updateAttr(row) {
      this.scene = 1;
      console.log(row);
      // 将修改的值赋值给attrForm可以让修改的属性展示
      this.attrForm = cloneDeep(row);

      // 由于row||attrValueList中缺少show属性 在修改的时候无法完成编辑模式的切换

      // 第一种方法:$set
      this.attrForm.attrValueList.forEach((item) => {
        this.$set(item, "show", false);
      });
      // 第二种方法
      //  this.attrForm.attrValueList= this.attrForm.attrValueList.map((item)=>{
      //     return {
      //       ...item,
      //       show:false
      //     }
      //   })
    },
    // 添加属性值的回调
    addAttrValue() {
      // 向数组attrValueList添加元素
      // 当点击添加属性值按钮的时候,向数组里面添加一个元素(代表的是一个新增的属性值)
      this.attrForm.attrValueList.push({
        attrId: undefined, //当前的属性的id值
        valueName: "", //属性知道的名字
        show: true, //给每一个属性值添加一个show,用来控制每一个属性指的模式切换(每个元素都有,相互不影响)
      });
      //  获取el-button组件让他自动聚焦[数组的最后一个元素]
      this.$nextTick(() => {
        this.$refs[this.attrForm.attrValueList.length - 1].focus();
      });
    },
    // 切换为查看
    toLook(row) {
      // 判断当失去焦点的时候：属性值不能为空
      if (row.valueName.trim() == "") {
        this.$message({
          type: "warning",
          message: "请不要输入空内容",
        });
        return;
      }
      // 查看新增的属性值是否出现过
      let repeat = this.attrForm.attrValueList.find((item) => {
        // 判断是否重复的时候:将用户当前的这个属性值抛出去
        if (item != row) {
          return item.valueName == row.valueName;
        }
      });
      if (repeat) {
        this.$message({
          type: "warning",
          message: "请不要输入重复内容",
        });
        return;
      }

      // 失去焦点
      row.show = false;
    },
    // 切换编辑模式
    toEidt(row, index) {
      // 变为编辑模式
      row.show = true;
      // 通过ref获取input组件,让它聚焦
      this.$nextTick(() => {
        this.$refs[index].focus();
      });
    },
    // 发送添加及修改的请求
    async addOrUpdateSave() {
      // 整理参数
      // 属性值为空的不带给服务器
      // 同事show应该页删除
      this.attrForm.attrValueList = this.attrForm.attrValueList.filter(
        (item) => {
          if (item.valueName != "") {
            delete item.show;
            return true;
          }
        }
      );
      try {
        await this.$ajax.attr.reqAddOrUpdateAttr(this.attrForm);
        this.scene = 0;
        this.getAttrList();
      } catch (error) {
        this.$message({
          type: "error",
          message: "提交失败",
        });
      }
    },
  },
};
</script>

<style>
</style>